#include "Components/ComponentBlockEntity.h"
#include "Engine/GameGuid.h"
#include "GameSingleton.h"
#include "GameRegistry.hpp"
#include "Log.h"

#include "Game/TableComponent.hpp"


using namespace PocketSurvival::Engine;

namespace PocketSurvival
{
    static const std::string componentName = "BlockEntity";
	const std::string &ComponentBlockEntity::getName() const
	{
		return componentName;
	}
    const std::string &ComponentBlockEntity::GetName()
    {
        return componentName;
    }
    void ComponentBlockEntity::load(const nlohmann::json &jsonData)
    {
        coordinates = jsonData["Coordinates"].get<Point3>();
        std::vector<IInventory *> inventoryVec;
        
        TableComponent *tableCom = GameSingleton::gameRegistry->get<TableComponent*>(entity);
        tableCom->dynamicFindComponent<IInventory>(inventoryVec);
        if (inventoryVec.size() > 0)
        {
            if (inventoryVec.size() > 1)
            {
                Log::Error(fmt::format("[ComponentBlockEntity]出现多个 IInventory 情况"));
            }
            inventory = inventoryVec[0];
            inventory->blockEntity = this;
        }
    }

    void ComponentBlockEntity::save(nlohmann::json &jsonData)
    {
        jsonData["Coordinates"] = coordinates;
    }
    void ComponentBlockEntity::saveToMsgJson(nlohmann::json &jsonData)
    {
        MSG_VALUE_SET(jsonData, "Coordinates", "Point3", coordinates.toString());
        MSG_VALUE_SET(jsonData, "Owner", "System.Guid", GameGuid::GetZeroGuid().toString());
    }
} // namespace PocketSurvival
